//应用层 传输层 网络层 数据链路层 物理层
#include <arpa/inet.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
using namespace std;
// uint32_t htonl(uint32_t hostlong);
// uint16_t htons(uint16_t hostshort);
// uint32_t ntohl(uint32_t netlong);
// uint16_t ntohs(uint16_t netshort);

class Socket
{
public:
    //失败返回-1同时错误码被设置，成功返回一个文件描述符
    Socket()
    {
        _sock = socket(AF_INET, SOCK_STREAM, 0);
        assert(_sock > 0);
    }
    void Bind()
    {
        struct sockaddr_in local;
        memset(&local, '\0', sizeof(local));
        local.sin_family = AF_INET;//协议家族
        local.sin_port = htons(8888);//端口号
        local.sin_addr.s_addr = inet_addr("127.0.0.0");//ip地址
        int x = bind(_sock, (struct sockaddr *)&local, sizeof(sockaddr));
        assert(x > 0);
    }
    void Listen()
    {
        int x = listen(_sock, 1);
        assert(x > 0);
    }
    int Sock()
    {
        return _sock;
    }
    ~Socket()
    {
        assert(_sock > 0);
        close(_sock);
    }

private:
    int _sock;
};
